//
//  SuccessLoginTableViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/27.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "SuccessLoginTableViewController.h"
#import "BaseTableViewCell.h"
#import "SuccessLoginViewCell.h"
#import "SuccessLoginViewCell2.h"
#import "UIImageView+WebCache.h"
#import "RegisterViewController.h"
#import "ExchangePassword.h"
#import "NIghtCell.h"
#import "UIImageView+WebCache.h"
#import "CollectViewController.h"

@implementation SuccessLoginTableViewController

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];


}

-(void)viewDidLoad{
    
    
  
    
    
    
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.title = @"个人中心";
     self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.hidesBackButton = YES;
    
    self.arrayOfList = @[@"    ",@"     更改密码",@"     退出登录",@"     我的收藏",@"     清空缓存",@"     清空收藏",@"     清空登录密码",@"     关于我们"];
    
    
    [self.tableView registerClass:[SuccessLoginViewCell class] forCellReuseIdentifier:@"sucess"];
    
    
    [self.tableView registerClass:[SuccessLoginViewCell2 class] forCellReuseIdentifier:@"sucess2"];
    
    [self.tableView registerClass:[NIghtCell class] forCellReuseIdentifier:@"NIghtCell"];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
}

- (void)back{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
        
    }else{
        
        return 8;
        
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        SuccessLoginViewCell *successCell = [self.tableView dequeueReusableCellWithIdentifier:@"sucess"];
        successCell.labelOfName.text = self.name;
        successCell.labelOfCards.text = self.cards;
        successCell.labelOfLevel.text = self.level;
        successCell.labelOfGiftCards.text =self.integral;
        
        NSURL *url = [NSURL URLWithString:self.photo];
        
        [successCell.imageViewOfAvatar sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        
        
        return successCell;
        
    }else{
        
        if (indexPath.row == 0) {
            
            
            
            
            
            NIghtCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"NIghtCell"];
            cell.labelOfInformation.text = @"     夜间模式";
            
            
            NSUserDefaults *nightIsOn = [NSUserDefaults standardUserDefaults];
            
            NSString *isOn = [nightIsOn objectForKey:@"night"];
            if ([isOn isEqualToString:@"0"]) {
                cell.switchOfCell.on = NO;
            }else if([isOn isEqualToString:@"1"]){
                cell.switchOfCell.on = YES;
            }
            cell.switchOfCell.tag = 40000;
            [cell.switchOfCell addTarget:self action:@selector(click) forControlEvents:UIControlEventValueChanged];
            
            return cell;
            
        }
        
        
        SuccessLoginViewCell2 *successCell2 = [self.tableView dequeueReusableCellWithIdentifier:@"sucess2"];
        
        successCell2.labelOfInformation.text = self.arrayOfList[indexPath.row];
        
        return successCell2;
        
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        return 160;
        
    }else{
        
        return 50;
    }
    
    
}

#pragma nark - 更改密码
#pragma mark - 退出当前账号

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1 && indexPath.row == 1) {
        
        
        ExchangePassword *exchangePasswordViewController = [[ExchangePassword alloc]init];
        
        [self.navigationController pushViewController:exchangePasswordViewController animated:YES];
        
        
        
    }else if(indexPath.section == 1 && indexPath.row == 2){
        
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"确认退出?" message:@"真的要退出吗?" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"是的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
            [self.navigationController popViewControllerAnimated:YES];
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"我再看看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        
        [self presentViewController:alert animated:YES completion:nil];
        
        
        
    }
    
    else if(indexPath.section == 1 && indexPath.row == 3){
        
        CollectViewController *collect = [[CollectViewController alloc]init];
        
        [self.navigationController pushViewController:collect animated:YES];
        
        
    }
    
    else if(indexPath.section == 1 && indexPath.row == 4){
        
        
        //计算检查缓存大小
        
        NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
        //
        NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
        
        
        NSLog(@"%@",currentVolum);
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认删除缓存?" message:currentVolum preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"不删除" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"删除" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self clearTmpPics];
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }
    
    
    else if(indexPath.section == 1 && indexPath.row == 5){
        
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认删除收藏?" message:@"删除后不能恢复了哦!" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"不删除" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"删除" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self clearCollect];
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];


    }
    else if(indexPath.section == 1 && indexPath.row == 6){
   
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认删除缓存?" message:@"删除后下次不能自动登录了哦!" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"不删除" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"删除" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"email"];
         
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
     
    }

}

//清空缓存

-(void)clearCollect{

    
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *sqlFilePath = [documentPath stringByAppendingPathComponent:@"cigarette.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    
            // 数据库删除
    [fileManager removeItemAtPath:sqlFilePath error:nil];




}
//计算出大小
- (NSString *)fileSizeWithInterge:(NSInteger)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}

//清除SD&网络缓存
- (void)clearTmpPics

{
    
    [[SDImageCache sharedImageCache] clearDisk];
    
    [[SDImageCache sharedImageCache] clearMemory];//可有可无
    
}

- (void)click{
    
    UISwitch *switchCell = [self.view viewWithTag:40000];
    
    
    if (switchCell.on == YES) {
        
        NSUserDefaults  *nightIsOn = [NSUserDefaults standardUserDefaults];
        
        NSString *isOn = @"1";
        
        [nightIsOn setObject:isOn forKey:@"night"];
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NIGHTON"
                          object:nil];
        
        
        
    }else{
        
        NSUserDefaults  *nightIsOn = [NSUserDefaults standardUserDefaults];
        
        NSString *isOn = @"0";
        
        [nightIsOn setObject:isOn forKey:@"night"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NIGHTOFF"
                          object:nil];
        
    }

    
}
@end
